package com.gxsyh.nantie.order.provider.service.impl;

import javax.annotation.Resource;

import com.gxsyh.nantie.order.provider.domain.TbSubsidy;
import com.gxsyh.nantie.order.provider.mapper.TbSubsidyMapper;
import com.gxsyh.nantie.order.provider.TbSubsidyService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @email yom535@outlook.com
 * @author: 刘锋嘉(Liu Fengjia)
 * @date: 2020/2/17
 * @time: 15:43
 * @Description:
 */
@Transactional
@Service(version = "1.0.0")
public class TbSubsidyServiceImpl implements TbSubsidyService {

    @Resource
    private TbSubsidyMapper tbSubsidyMapper;

    @Override
    public List<TbSubsidy> selectSubsidyByDepartment(Long departmentId) {
        return tbSubsidyMapper.selectSubsidyByDepartment(departmentId);
    }

    @Override
    public boolean selectTimeOnSubsidy(Long id) {
        Example example=new Example(TbSubsidy.class);
        example.createCriteria().andEqualTo("id",id);
        TbSubsidy tbSubsidy = tbSubsidyMapper.selectOneByExample(example);
        return thinkTime(tbSubsidy.getStartime(), tbSubsidy.getEndtime());
    }

    @Override
    public TbSubsidy selectSubsidy(Long id) {
        Example example=new Example(TbSubsidy.class);
        example.createCriteria().andEqualTo("id",id);
        return tbSubsidyMapper.selectOneByExample(example);
    }


    public static boolean thinkTime(String beginTimes, String endTimes) {

        if ("00:00".equals(beginTimes)&& "00:00".equals(endTimes))
        {
            return true;
        }

        int beginT = Integer.parseInt(beginTimes.substring(0, 2));
        int endT = Integer.parseInt(endTimes.substring(0,2));
        if (beginT>endT)
        {
            java.util.Calendar cal=java.util.Calendar.getInstance();
            cal.setTime(new Date());
            cal.add(Calendar.DATE,1);
            Date nextDate=cal.getTime();
            String next_dateStr = new SimpleDateFormat("yyyy-MM-dd").format(nextDate);
            StringBuilder sb = new StringBuilder();
            sb.append(next_dateStr);
            sb.append(" "+endTimes);

            SimpleDateFormat ends = new SimpleDateFormat("yyyy-MM-dd HH:mm");

            SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
            //初始化
            Date nowTime = null;
            Date beginTime = null;
            Date endTime = null;
            try {
                //格式化当前时间格式
                nowTime = df.parse(df.format(new Date()));
                //定义开始时间
                beginTime = df.parse(beginTimes);
                //定义结束时间
                endTime = ends.parse(sb.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            //用判断方法
            boolean flag = belongCalendar(nowTime, beginTime, endTime);
            //输出为结果
            if (flag) {
                //处于规定的时间段内，执行对应的逻辑代码
                return flag;
            } else {
                //不处于规定的时间段内，执行对应的逻辑代码
                return flag;

            }
        }

        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
//初始化
        Date nowTime = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            //格式化当前时间格式
            nowTime = df.parse(df.format(new Date()));
            //定义开始时间
            beginTime = df.parse(beginTimes);
            //定义结束时间
            endTime = df.parse(endTimes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //用判断方法
        boolean flag = belongCalendar(nowTime, beginTime, endTime);
        //输出为结果
        if (flag) {
            //处于规定的时间段内，执行对应的逻辑代码
            return flag;
        } else {
            //不处于规定的时间段内，执行对应的逻辑代码
            return flag;

        }
    }

    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        //设置当前时间
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        //设置开始时间
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        //设置结束时间
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        //处于开始时间之后，和结束时间之前的判断
        return date.after(begin) && date.before(end);
    }
}

